//
//  UIButton+HHCategory.m
//  Flower
//
//  Created by 花花 on 2016/12/2.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "UIButton+HHCategory.h"
#import <objc/runtime.h>
#define kpadding 5

static void *DragEnableKey = &DragEnableKey;
static void *AdsorbEnableKey = &AdsorbEnableKey;
@implementation UIButton (HHCategory)

-(void)setDragEnable:(BOOL)dragEnable{
    objc_setAssociatedObject(self,DragEnableKey, @(dragEnable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)isDragEnable{

    return [objc_getAssociatedObject(self, DragEnableKey)boolValue];
}

-(void)setAdsorbEnable:(BOOL)adsorbEnable{
 objc_setAssociatedObject(self,AdsorbEnableKey, @(adsorbEnable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}
-(BOOL)isAdsorbEnable{

 return [objc_getAssociatedObject(self, AdsorbEnableKey)boolValue];

}
CGPoint beginPoint;
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    self.highlighted = YES;
    if (![objc_getAssociatedObject(self, DragEnableKey)boolValue]) {
        return;
    }
    //拿到手指移动的位置
    UITouch *touch = [touches anyObject];
    beginPoint =[touch locationInView:self];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.highlighted = NO;
    
    if (![objc_getAssociatedObject(self, DragEnableKey)boolValue]) {
        return;
    }

    UITouch *touch = [touches anyObject];
    CGPoint  nowPoint =[touch locationInView:self];
    CGFloat offsetX = nowPoint.x - beginPoint.x;
    CGFloat offsetY = nowPoint.y - beginPoint.y;
    
    self.center = CGPointMake(self.center.x +offsetX, self.center.y+offsetY);
    
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    if (self.highlighted) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
        self.highlighted = NO;
    }
    
    
    if (self.superview && [objc_getAssociatedObject(self, AdsorbEnableKey)boolValue]) {
        
        CGFloat marginLeft = self.frame.origin.x;
        
        CGFloat marginRight = self.superview.width -self.x - self.width;
        
        CGFloat marginTop = self.y;
        
        CGFloat marginBottom = self.superview.height-self.y - self.height;
        
        [UIView animateWithDuration:0.25 animations:^{
           
            if (marginTop<60) {
                self.frame = CGRectMake(marginLeft<marginRight?marginLeft<kpadding?kpadding:self.x:marginRight<kpadding?self.superview.width-kpadding:self.x, kpadding, self.width, self.height);
            }else if (marginBottom<60){
            
                self.frame =  CGRectMake(marginLeft<marginRight?marginLeft<kpadding?kpadding:self.x:marginRight<kpadding?self.superview.width-self.width-kpadding:self.x, self.superview.width-self.width- kpadding, self.width, self.height);
                
            }else{
            
            
                self.frame = CGRectMake(marginLeft<marginRight?kpadding:self.superview.width-self.width-kpadding, self.y, self.width, self.height);
            }
            
        }];
    }
    
    
    

}

@end
